home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-06-10 | 4.9 KB | 198 lines | [TEXT/MPCC] |
- /*
- EXAMPLE OF EXCEPTION HANDLING USING MW 6.0 - Kent Sandvik
- 5/24/95
-
- Fun with exception handlers for handling unexpected events inside a C++ framework.
-
-
- TODO:
- Add set_unexpected when this is supported.
-
- ISSUES:
- Note this code requires a compiler that handles the basic C++ exception model
- (MW 6.0 or later, Symantec C++ compiler that handles compiler level
- exception handling).
-
- */
-
- // ______________________________________________________________________________
- // INCLUDES
- #include <iostream>
-
-
- // ______________________________________________________________________________
- // DEFINES
- #define DEBUG true
- // #define CATCHEVERYTHING
- #define CATCHSPECIFIC
-
-
- // ______________________________________________________________________________
- // MACROS
-
- // This macro will automatically fill in the __FILE__ and __LINE statements
- // when a throw is used.
-
- #define THROWEXCEPTION(name, number) \
- throw TMacException( (name), (number), __FILE__, __LINE__)
-
- #define THROWSERIOUSEXCEPTION(name, number) \
- throw TSeriousMacException( (name), (number), __FILE__, __LINE__)
-
-
- // ______________________________________________________________________________
- // EXCEPTION CLASSES
-
- class TMacException
- {
- public:
- TMacException(const char *theMessage, const OSErr theError) :
- fMessage(theMessage),
- fError(theError),
- fFileName("NO FILE SPECIFIED"),
- fLineNumber(0L)
- {};
-
- TMacException(const char *theMessage, const OSErr theError, const char *theFileName, const long theLineNumber) :
- fMessage(theMessage),
- fError(theError),
- fFileName(theFileName),
- fLineNumber(theLineNumber)
- {};
-
- const char* GetExceptionMessage(void) { return fMessage;};
- const OSErr GetExceptionOSErr(void) { return fError;};
- const char* GetExceptionFile(void) { return fFileName;};
- const long GetExceptionLine(void) { return fLineNumber;};
-
- protected:
- const OSErr fError;
- const char* fMessage;
- const char* fFileName;
- const long fLineNumber;
- };
-
-
- class TSeriousMacException : public TMacException
- {
- public:
- TSeriousMacException(const char *theMessage, const OSErr theError) :
- TMacException(theMessage, theError, "NO FILE SPECIFIED", 0L)
- {};
-
- TSeriousMacException(const char *theMessage, const OSErr theError, const char *theFileName, const long theLineNumber) :
- TMacException(theMessage, theError, theFileName, theLineNumber)
- {};
-
- };
-
- // ______________________________________________________________________________
- // TERMINATE DEFAULT FUNCTIONS
-
- void HandleTerminate(void);
- void HandleTerminate(void)
- {
- cout << "This is the my own terminate function that I've installed!" << endl;
- }
-
-
- // ______________________________________________________________________________
- // USER CLASSES
-
- class Foo {
- public:
- Foo();
- void DoASillyThing(void);
- };
-
-
- Foo::Foo()
- {
- // cout << "we are inside the Foo constructor" << endl;
-
- }
-
- void Foo::DoASillyThing(void)
- {
- // Uncomment the test you want to try out:
-
- // throw TMacException("We did a silly thing", noErr);
- // throw TMacException("We did a silly thing", noErr, __FILE__, __LINE__);
- // throw TSeriousMacException("We did a really serious, silly thing", noErr);
- // throw TSeriousMacException("We did a really serious, silly thing", noErr, __FILE__, __LINE__);
-
- // THROWEXCEPTION("We did a silly thing", noErr);
- THROWSERIOUSEXCEPTION("We did a really serious, silly thing", noErr);
- // throw "This is an exception that we have not specified!";
- }
-
-
- class Bar {
- public:
- Bar();
- };
-
-
- Bar::Bar()
- {
- // cout << "We are inside the Bar constructor" << endl;
- throw TMacException("Inside the Bar constructor", noErr);
- }
-
-
- // ______________________________________________________________________________
- // MAIN
-
- void main (void)
- {
- Foo* aFoo;
- Bar* aBar;
-
- // set_terminate(HandleTerminate);
-
- try
- {
- aFoo = new Foo;
-
- aFoo->DoASillyThing();
-
- aBar = new Bar;
- }
-
-
- // Our catch statements, note that these should be ordered from specific to generic, inheritance wise,
- // this because the first valid exception handler is called, and the others will be ignored.
-
- #ifdef CATCHSPECIFIC
- catch (TSeriousMacException &ex)
- {
- long dontWriteToNull;
- #ifdef DEBUG
- cerr << "SERIOUS EXCEPTION: " << ex.GetExceptionMessage() << ", OSErr: " << ex.GetExceptionOSErr() <<
- ", File: " << ex.GetExceptionFile() << ", Line: " << ex.GetExceptionLine() << endl;
- #endif //DEBUG
- // Use real Mac UI to signal about the seriousness to the user of the application.
- Delay(3*60, &dontWriteToNull);
- ExitToShell();
-
- }
-
- catch(TMacException &ex)
- {
- #ifdef DEBUG
- cerr << "EXCEPTION: " << ex.GetExceptionMessage() << ", OSErr: " << ex.GetExceptionOSErr() <<
- ", File: " << ex.GetExceptionFile() << ", Line: " << ex.GetExceptionLine() << endl;
- #endif //DEBUG
- // Use real Mac UI to signal about the problem to the user of the application.
- }
- #endif // CATCHSPECIFIC
-
-
- #ifdef CATCHEVERYTHING
- catch (...) // catch everything
- {
- cerr <<"We catch every single exception here.\n";
- }
- #endif // CATCHEVERYTHING
-
- }